﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKArch.DimensionModule
{
	public partial class WallCenterDimensionForm : System.Windows.Forms.Form
	{
		public int SelectDimTypeIndex
		{
			get
			{
				return this.cmbAlignDimType.SelectedIndex;
			}
		}

		public bool LableGrids
		{
			get
			{
				return this.chbLabelGrids.Checked;
			}
		}

		public WallCenterDimensionForm(List<string> alignDimTypeList)
		{
			this.m_AlignDimTypeList = alignDimTypeList;
			this.InitializeComponent();
			foreach (string text in this.m_AlignDimTypeList)
			{
				if (!this.cmbAlignDimType.Items.Contains(text))
				{
					this.cmbAlignDimType.Items.Add(text);
				}
			}
			if (this.cmbAlignDimType.Items.Count > 0)
			{
				this.cmbAlignDimType.SelectedIndex = 0;
			}
			this.SetWidth(this.cmbAlignDimType);
		}

		private void WallCenterDimensionForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void WallCenterDimensionForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveData();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void WallCenterDimensionForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			WallCenterDimensionForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			WallCenterDimensionForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void WallCenterDimensionForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				WallCenterDimensionForm.SendMessage(CmdWallCenterDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "WallCenterDimensionForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.chbLabelGrids.Checked = true;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			WallCenterDimensionForm.m_Version = Convert.ToInt32(streamReader.ReadLine());
			if (WallCenterDimensionForm.m_Version == 1)
			{
				int num = Convert.ToInt32(streamReader.ReadLine());
				if (num < this.cmbAlignDimType.Items.Count)
				{
					this.cmbAlignDimType.SelectedIndex = num;
				}
				this.chbLabelGrids.Checked = Convert.ToBoolean(streamReader.ReadLine());
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "WallCenterDimensionForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(WallCenterDimensionForm.m_Version);
			streamWriter.WriteLine(this.cmbAlignDimType.SelectedIndex);
			streamWriter.WriteLine(this.chbLabelGrids.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void SetWidth(System.Windows.Forms.ComboBox cbx)
		{
			int num = 0;
			Graphics graphics = base.CreateGraphics();
			for (int i = 0; i < cbx.Items.Count; i++)
			{
				cbx.SelectedIndex = i;
				SizeF sizeF = graphics.MeasureString(cbx.Text, cbx.Font);
				if (num < (int)sizeF.Width)
				{
					num = (int)sizeF.Width;
				}
			}
			cbx.DropDownWidth = cbx.Width;
			if (cbx.DropDownWidth < num)
			{
				cbx.DropDownWidth = num;
			}
		}

		private List<string> m_AlignDimTypeList;

		private static int m_Version = 1;
	}
}
